package com.geeksaga.dao.ibatis.mysql;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.memory.UserAttribute;
import org.springframework.stereotype.Repository;

import com.geeksaga.crypt.TripleDes;
import com.geeksaga.dao.UserDao;
import com.geeksaga.dao.ibatis.BaseIBatisDao;
import com.geeksaga.domain.User;
import com.geeksaga.security.UserGrantedAuthority;

/**
 * @author newbie
 * @since 0.1
 */
@Repository
public class UserDaoImpl extends BaseIBatisDao implements UserDao {
	
	protected final Log logger = LogFactory.getLog(getClass());

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#add(com.geeksaga.domain.User)
	 */
	public User add(User user) throws DataAccessException {
		return (User) getSqlMapClientTemplate().insert("USER.add", user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#find(com.geeksaga.domain.User)
	 */
	public User find(User user) throws DataAccessException {
		return null;// (User) getJdbcTemplate().queryForObject(sql, new Object[] { user.getPid(), user.getUserId() },
					// getRowMapper());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#selectList()
	 */
	@SuppressWarnings("unchecked")
	public List<User> selectList() throws DataAccessException {
		return (List<User>) getSqlMapClientTemplate().queryForList("USER.selectList");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#findList(com.geeksaga.domain.User)
	 */
	public List<User> findList(User user) throws DataAccessException {
		return null;// (List<User>) getJdbcTemplate().query(sql, new Object[] { user.getSid() }, getRowMapper());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#count()
	 */
	public int count() throws DataAccessException {
		return (Integer) getSqlMapClientTemplate().queryForObject("USER.count");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#modify(com.geeksaga.domain.User)
	 */
	public int modify(User user) throws DataAccessException {
		return getSqlMapClientTemplate().update("USER.update", user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#remove(com.geeksaga.domain.User)
	 */
	public User remove(User user) throws DataAccessException {
		return user;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.geeksaga.dao.UserDao#authenticate(com.geeksaga.domain.User)
	 */
	@SuppressWarnings("unchecked")
	public User authenticate(User user) throws DataAccessException, Exception {
		Map<String, String> param = new HashMap<String, String>();
		param.put("pid", user.getPid());
		param.put("userId", user.getUserId());

		List<User> list = (List<User>) getSqlMapClientTemplate().queryForList("USER.authenticate", param);
		User authenrityUser = null;
		UserAttribute userAttributes = new UserAttribute();

        for (User tempoprayUser : list) {
            tempoprayUser.setPassword(new TripleDes().decrypt(tempoprayUser.getPasswd()));
            
            authenrityUser = tempoprayUser;

            UserGrantedAuthority authority = new UserGrantedAuthority(tempoprayUser.getAuthority());

            if (userAttributes.getAuthorities().contains(authority)) {
                int index = userAttributes.getAuthorities().indexOf(authority);
                authority = (UserGrantedAuthority) userAttributes.getAuthorities().get(index);
            } else {
                userAttributes.addAuthority(authority);
            }

            authenrityUser.setAuthorities(userAttributes.getAuthorities());
        }		

		return authenrityUser;
	}
}